{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Message\n",
    "\n",
    "本文档描述 Assistants API中与 Message 相关的接口。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境准备\n",
    "\n",
    "首先需要安装AppBuilder-SDK代码库，若已在开发环境安装，则可跳过此步。\n",
    "\n",
    "**注意：**: appbuilder-sdk 的python版本要求 3.9+，安装的SDK version >= 0.7.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "shellscript"
    }
   },
   "outputs": [],
   "source": [
    "!python3 -m pip install appbuilder-sdk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import appbuilder\n",
    "\n",
    "# 配置你的密钥，主要在这之前需要首先申请Assistant API的内测资格\n",
    "os.environ[\"APPBUILDER_TOKEN\"] = \"your_appbuilder_token\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Message相关函数\n",
    "\n",
    "### 创建Message\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "创建Message附加到指定Thread末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先创建一个对话thread\n",
    "thread = appbuilder.assistant.threads.create()\n",
    "\n",
    "# 创建Message附加到指定Thread末尾i\n",
    "msg = appbuilder.assistant.threads.messages.create(\n",
    "    thread_id=thread.id,\n",
    "    content=\"hello world?\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询thread下的message列表\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "查询指定Thread下的Message列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='msg_d563991f8383424aa9334a24fb06b713' object='thread.message' role=<AssistantMessageRole.USER: 'user'> content=[AssistantContent(type='text', text=AssistantText(value='hello world?', annotations=[]))] created_at=1717645387637 thread_id='thread_a361a66e3cb94928b3b6050789d55dae' assistant_id='' run_id='' file_ids=[]\n"
     ]
    }
   ],
   "source": [
    "# 查询相应thread下的message列表，你可以指定limit来控制返回的message数量，默认值为20\n",
    "msg_list = appbuilder.assistant.threads.messages.list(\n",
    "    thread_id=msg.thread_id,\n",
    "    limit=10\n",
    ") \n",
    "\n",
    "# 打印查询到的message信息\n",
    "for msg_data in msg_list.data:\n",
    "    print(msg_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询指定message\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "根据message_id查询指定Message的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id='msg_d563991f8383424aa9334a24fb06b713' object='thread.message' role=<AssistantMessageRole.USER: 'user'> content=[AssistantContent(type='text', text=AssistantText(value='hello world?', annotations=[]))] created_at=1717645387637 thread_id='thread_a361a66e3cb94928b3b6050789d55dae' assistant_id='' run_id='' file_ids=[]\n"
     ]
    }
   ],
   "source": [
    "# 提供thread_id与message_id查询message的信息\n",
    "msg_query = appbuilder.assistant.threads.messages.query(\n",
    "    thread_id=msg.thread_id,\n",
    "    message_id=msg.id\n",
    ")\n",
    "\n",
    "# 打印查询到的message信息\n",
    "print(msg_query)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改message对象\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "修改Message对象，允许content和file_ids字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将content更新为'你好'\n",
    "msg_update= appbuilder.assistant.threads.messages.update(\n",
    "    thread_id=msg.thread_id,\n",
    "    message_id=msg.id,\n",
    "    content='你好'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询message下的文件列表\n",
    "\n",
    "#### 功能介绍\n",
    "\n",
    "查询一个Message对象下挂载的的File文件列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询一个Message对象下挂载的的File文件列表，可以通过设置līmit参数来控制返回File的数量，默认返回20个File对象\n",
    "msg_files = appbuilder.assistant.threads.messages.files(\n",
    "    thread_id=msg_update.thread_id,\n",
    "    message_id=msg_update.id,\n",
    "    limit=10\n",
    ")\n",
    "\n",
    "# 打印查询到的File文件信息\n",
    "for file_data in msg_files.data:\n",
    "    print(file_data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
